//https://github.com/antlr/grammars-v4
grammar Calc;

// parser
prog: stmt;

stmt:   expr                            # printExpr
    |   ID '=' expr                     # assign
    |   NEWLINE                         # blank
    ;

expr:   <assoc=right> expr op='^' expr  # pow
    |   expr op=('*'|'/') expr          # mulDiv
    |   expr op=('+'|'-') expr          # addSub
    |   NUM                             # int
    |   ID                              # id
    |   '(' expr ')'                    # parens
    ;

// lexer
MUL : '*';
DIV : '/';
ADD : '+';
SUB : '-';
ID  : Letter LetterOrDigit*;
NUM : Number;
fragment Letter: [a-zA-Z_];
fragment Digit : ('0' .. '9') +;
fragment Number: ('0' .. '9') + ('.' ('0' .. '9') +)?;
fragment LetterOrDigit: Letter | Digit;
NEWLINE: '\r'? '\n';
WS  : [ \t]+ -> skip;